package com.flag.liutao.common.handler;

import com.flag.liutao.common.domain.ApiCode;
import com.flag.liutao.common.domain.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class MyExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public ResponseResult handle(Exception e){
        log.error(e.getMessage());
        e.printStackTrace();
        return ResponseResult.fail(e.getMessage());
    }

    @ExceptionHandler(MyException.class)
    @ResponseBody
    public ResponseResult myHandle(MyException e){
        log.error("MyException==========="+e.getMsg());
        e.printStackTrace();
        return ResponseResult.fail(e.getMsg());
    }

    @ExceptionHandler(AccessDeniedException.class)
    @ResponseBody
    public ResponseResult myAccessDeniedExceptionHandle(AccessDeniedException e){
        log.error("权限不足");
        return ResponseResult.fail(ApiCode.NOT_PERMISSION.getCode(),ApiCode.NOT_PERMISSION.getMessage());
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public ResponseResult myMethodArgumentNotValidExceptionHandle(MethodArgumentNotValidException e){
        log.error("认证错误");
        return ResponseResult.fail(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }


}
